package com.touch.jgloo.owt;
import com.touch.jgloo.core.*;
import com.sun.opengl.util.texture.*;
/**
 * JGLSprite represents a sprite. Basically, its just a quad used for tiling in sequence
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public class JGLSprite extends JGLQuad
{
//	width and height of the Sprite
	private float width, height;
	//u-v values of the Sprite's texture
	private float u1, u2, v1, v2;
	//ratio of width and height compared to 32x32
	private float ratioW, ratioH;
	//vertices of the sprite, 4 sets of 3 dimensional points
	private float verts[] = new float[12];
	//frames across in the texture
	private int framesAcross;
	//frames down in the texture
	private int framesDown;
	
	/**
	 * Sets the texture of the sprite. Requires number of frames in the sprite.	
	 * @param texture Texture to use for the sprite
	 * @param frames Frames across to pull from the texture
	 * @since r461
	 */
	public final  void setTexture(final Texture texture, final int framesAcross, final int framesDown)
	{
		//get the Sprite as a polygon
		final JGLPolygon polygon = getPolygon();
		//set the texture of the polygon to the Sprite sheet used
		polygon.setTexture(texture);
		
		//set the frames across in the texture
		setFramesAcross(framesAcross);
		//set the frames down in the texture
		setFramesDown(framesDown);
		
		//set the width as a fraction of the texture's width to the frames across
		width 	= texture.getWidth()  / framesAcross;
		//set the height as a fraction of the texture's height to the frames down
		height 	= texture.getHeight() / framesDown;
		
		//set the width ratio of the sprite to 32x32
		ratioW = width / 32f;
		//set the height ratio of the sprite to 32x32
		ratioH = height / 32f;
		
		//modify the width of vertices 3 to match the new sprite size
		verts[3] = JGLConstants.TILERECT[3] * ratioW;
		//modify the width of vertices 6 to match the new sprite size
		verts[6] = JGLConstants.TILERECT[6] * ratioW;
		//modify the height of vertices 7 to match the new sprite size
		verts[7] = JGLConstants.TILERECT[7] * ratioH;
		//modify the height of vertices 10 to match the new sprite size
		verts[10]= JGLConstants.TILERECT[10]* ratioH;

		//build the quad using the vertex data
		setQuad(verts);
	}
	/**
	 * Sets the image of the sprite from a sub-image of the sprite sheet.
	 * @param xSource The left-most coordinate of the pixel of the sprite image
	 * @param ySource The top-most coordinate of the pixel of the sprite image
	 * @since r461
	 */
	public final  void setSprite(final int xSource, final int ySource)
	{
		//get the Sprite as a polygon
		final JGLPolygon polygon = this.getPolygon();
		
		/*                 U-V values
		 * U-V values pull a specific sub-image out of a larger image 
		 * then applies it to a texture. These are used to select 
		 * specific sprite frames for OpenGL.
		 */
		
		//set the first u value based off of the x-point and the width of the texture
		u1 = (xSource * width) / polygon.getTexture().getWidth();
		//set the first v value based off of the y-point and the height of the texture 
		v1 = (ySource * height) / polygon.getTexture().getHeight();
		//set the second u value based off of the x-point and the width of the texture 
		u2 = (xSource * width + width) / polygon.getTexture().getWidth();
		//set the second v value based off of the y-point and the height of the texture
		v2 = (ySource * height + height) / polygon.getTexture().getHeight();
		
		//set the U-V of point 1 of the polygon
		polygon.setUV(0, new JGLUV(u1, v1));
		//set the U-V of point 2 of the polygon
		polygon.setUV(1, new JGLUV(u2, v1));
		//set the U-V of point 3 of the polygon
		polygon.setUV(2, new JGLUV(u2, v2));
		//set the U-V of point 4 of the polygon
		polygon.setUV(3, new JGLUV(u1, v2));		
	}	
	/**
	 * Sets the number of frames in the sprite sheet
	 * @param frames Frames across in the sprite sheet
	 * @since r461
	 */
	public final void setFramesAcross(final int frames)
	{
		//modifies the number of frames across within the sprite sheet
		this.framesAcross = frames;
	}
	/**
	 * Sets the number of frames in the sprite sheet
	 * @param frames Frames down in the sprite sheet
	 * @since r461
	 */
	public final void setFramesDown(final int frames)
	{
		//modifies the number of frames down within the sprite sheet
		this.framesDown = frames;
	}
	/**
	 * Returns the number of frames in the sprite sheet
	 * @return Frames across that exist in the sprite sheet
	 * @since r461
	 */
	public final int getFramesAcross()
	{
		//return the number of frames across as an integer
		return framesAcross;
	}
	/**
	 * Returns the number of frames in the sprite sheet
	 * @return Frames down that exist in the sprite sheet
	 * @since r461
	 */
	public final int getFramesDown()
	{
		//return the numbre of frames down as an integer
		return framesDown;
	}	
}
